package de.hb.bv2.u01;

import ij.IJ;

import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Iterator;

abstract class Contour {
	int label;
	ArrayList<Node> nodes;

	Contour (int label, int initialSize) 
	{
		this.label = label;
		nodes = new ArrayList<Node>(initialSize);
	}

	void addNode (Node n)
	{
		nodes.add(n);
	}

	Shape makePolygon() 
	{
		int m = nodes.size();
		
		if (m>1)
		{
			int[] xPoints = new int[m];
			int[] yPoints = new int[m];
			int k = 0;
			Iterator<Node> itr = nodes.iterator();
			
			while (itr.hasNext() && k < m) 
			{
				Node cn = itr.next();
				xPoints[k] = cn.x;
				yPoints[k] = cn.y;
				k = k + 1;
			}
			
			return new Polygon(xPoints, yPoints, m);
		} else { // use circles for isolated pixels
			Node cn = nodes.get(0);
			
			return new Ellipse2D.Double(cn.x-0.1, cn.y-0.1, 0.2, 0.2);
		}
	}

	void moveBy (int dx, int dy) 
	{
		Iterator<Node> itr = nodes.iterator();
		
		while (itr.hasNext()) 
		{
			Node cn = itr.next();
			cn.moveBy(dx,dy);
		}
	}

	// debug methods:

	abstract void print();

	void printNodes ()
	{
		Iterator<Node> itr = nodes.iterator();
		
		while (itr.hasNext()) 
		{
			Node n = itr.next();
			IJ.write(" Node " + n.x + "/" + n.y);
		}
	}
}
